# Maintainer: Philippe Renon <philippe.renon@yahoo.fr>

_realname=rocksdb
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.10.2
pkgrel=1
pkgdesc="A library that provides an embeddable, persistent key-value store for fast storage (mingw-w64)"
arch=('any')
url="http://rocksdb.org"
license=("Apache")
depends=("${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-intel-tbb"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-snappy"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc")
options=('staticlibs' 'strip')
source=("https://github.com/facebook/${_realname}/archive/${_realname}-${pkgver}.tar.gz"
        "0001-fix-get-version-on-windows.patch"
        "0002-suppress-unused-warnings.patch"
        "0003-add-with-gflags-option.patch"
        "0004-fix-generating-and-install-import-lib.patch")
sha256sums=('11cbb016071cb79b0817866f30250bab851f5b069b13784af08de990a6f774b6'
            '31f7046c83e0f9afed64675f80e62245c631a22feaf9297898ab03ab3c94d25a'
            '5fcf5be787ada79ed29a8852c41f634118f00e9900838285eb0834b234f9e199'
            '3175e7acf36a053532c8249560738fd871f346461c42a03e7fc1625463fadc46'
            'ffb0954b95fd7e9e3b3609f6d168afeafd524376d96b9926d2ad53e5db3a7b78')

prepare() {
  cd "${srcdir}/${_realname}-${_realname}-${pkgver}"

  patch -p1 -i "${srcdir}/0001-fix-get-version-on-windows.patch"
  patch -p1 -i "${srcdir}/0002-suppress-unused-warnings.patch"
  patch -p1 -i "${srcdir}/0003-add-with-gflags-option.patch"
  patch -p1 -i "${srcdir}/0004-fix-generating-and-install-import-lib.patch"
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir ${srcdir}/build-${MINGW_CHOST}
  cd ${srcdir}/build-${MINGW_CHOST}

  declare -a extra_config
  if check_option "debug" "n"; then
    # tests don't compile in Release mode
    extra_config+=("-DCMAKE_BUILD_TYPE=Release" "-DWITH_TESTS=OFF" "-DWITH_GFLAGS=OFF")
  else
    # not tested + needs gflags dependency
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DWITH_ZLIB=ON \
    -DWITH_BZ2=ON \
    -DWITH_LZ4=ON \
    -DWITH_SNAPPY=ON \
    -DROCKSDB_INSTALL_ON_WINDOWS=ON \
    ../${_realname}-${_realname}-${pkgver}

  make DESTDIR=${pkgdir}
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

  local WINPREFIX=$(cygpath -wm ${MINGW_PREFIX})
  # Fix paths
  sed -i "s|${WINPREFIX}|${MINGW_PREFIX}|g" \
    ${pkgdir}${MINGW_PREFIX}/lib/cmake/rocksdb/RocksDBTargets.cmake

  install -Dm644 ${srcdir}/${_realname}-${_realname}-${pkgver}/LICENSE.Apache "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE
  install -Dm644 ${srcdir}/${_realname}-${_realname}-${pkgver}/LICENSE.leveldb "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE.leveldb
  install -Dm644 "${srcdir}/${_realname}-${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
